package image2;

import org.apache.commons.math3.analysis.interpolation.*;
import org.apache.commons.math3.analysis.polynomials.PolynomialFunctionLagrangeForm;
import org.apache.commons.math3.analysis.polynomials.PolynomialFunctionNewtonForm;
import org.apache.commons.math3.analysis.polynomials.PolynomialSplineFunction;

import java.util.Arrays;

public class InterpolationExample {
    public static void main(String[] args) {
        // 定义原始数据点的横纵坐标
        double[] x = {483d, 207d, 426d, 500d};
        double[] y = {190d, 199d, 144d, 249d};

        // 使用LinearInterpolator类创建插值函数
        SplineInterpolator li = new SplineInterpolator();
        PolynomialSplineFunction psf = li.interpolate(x, y);


        int newLength = 10;
        double[] newX = new double[newLength];
        for (int i = 0; i < newLength; i++) {
            newX[i] = x[0] + (x[x.length - 1] - x[0]) * i / (newLength - 1);
        }
        System.out.println(Arrays.toString(newX));

        // 计算新的数据点
        double[] newY = new double[newLength];
        for (int i = 0; i < newX.length; i++) {
            newY[i] = psf.value(newX[i]);
        }

        System.out.println(Arrays.toString(newY));


        System.out.println(Arrays.toString(x));
        System.out.println(Arrays.toString(y));
    }
}
